Uec3.Market4RepairStore = Ext.extend(Ext.app.Module,{
	id: 'market4RepairStore',
   /*
    * Initialize this module.
    * This function is called at startup (page load/refresh).
    */
   init : function(initParams){
   
   },
   
   getComponent:function(params){
   	    
   	    var marketId;
   	    var repairStore_form = new Ext.FormPanel({
   	         border:false,
   	         frame:true ,
   	         buttons:[
   	          {text:'确定购买',handler:buyPackage},
   	          {text:'取消',handler:function(){
   	             this.ownerCt.ownerCt.ownerCt.close();
   	          }}
   	         ],
   	         buttonAlign:'center'
   	    });
   	    
   	    var getMarket4RepairStore = function(){
   	         var market_store = new Ext.data.JsonStore({
   	         		proxy:new Ext.data.HttpProxy({ 
    					url:Uec3.App.Constants.homeUrl + "/market/selectMarket4RepairStore.do" // see options parameter for Ext.Ajax.request					 					    
					}),
   	          		//url:Uec3.App.Constants.homeUrl + "/market/selectMarket4RepairStore.do",
   	          		root:'markets', 
   	          		autoLoad:true,
   	          		fields:['id','name','description'],
   	          		listeners:{
   	            	'beforeload':function(store, options){   	            		
                    	Ext.MessageBox.wait("正在加载市场列表...","提示");                   	
                 	},
                 	'load':function(store,records,options){
                 		Ext.MessageBox.hide();
                 		var marketName = store.reader.jsonData.marketName;
                 		marketId = store.reader.jsonData.marketId;
                 		var label = new Ext.form.Label({
                 			text:'您的主市场是'+marketName+",请再选择两个市场,您可以在这些市场里搜索零件",
                 			style: {font:'normal 13px arial,serif'}
                 		});
                 		
                 		var market_checkGroup = [];
                 		Ext.each(records,function(record,index,records){
                 			var item = {boxLabel:record.data.name+'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp'+record.data.description,name:'marketIds',inputValue:record.data.id};
                 			if(record.data.name == marketName){
                 			  item.checked = true ;
                 			  item.disabled = true;
                 			  item.itemId="checkedBox";
                 			}
                 			market_checkGroup.push(item);
                 		});
                 		var checkGroup = new Ext.form.CheckboxGroup({
   	                     	fieldLabel : '市场列表',  	                     	
   	                     	allowBlank : false, 
   	                     	columns: 1, 
   	                     	itemId:'checkGroup',
   	                     	//vertical:true,
                         	items:market_checkGroup,
                         	listeners:{
                         	   'change':function(checkboxGroup,checked){
                         	       if(checked.length > 3){
                         	          Ext.MessageBox.alert("提示","只能选择两个市场!");
                         	          checkboxGroup.reset();
                         	       }
                         	   }
                         	}
   	                 	});
   	                 	repairStore_form.add(label);
   	                 	repairStore_form.add(checkGroup);
   	                 	repairStore_form.doLayout();
                 	}
   	          }
   	        });
   	    }
   	    
   	    function buyPackage(){  	    	
   	    	Ext.MessageBox.confirm("提示","确定购买此套餐?",function(btnId){
   	    	   if(btnId=='yes'){
   	    	   	  var myMask = new Ext.LoadMask(repairStore_form.ownerCt.el,{msg:'正在处理请求,请稍后.....'});
   	    	   	  myMask.show();
   	    	   	  repairStore_form.enable();  	    	   	
   	    	   	  var checkedIds = repairStore_form.getForm().getValues();
   	    	   	  var checkGroups = repairStore_form.getComponent("checkGroup").items.items;
   	    	   	  Ext.each(checkGroups,function(item,index){
   	    	   	  		if(item.inputValue == marketId){
   	    	   	  		   item.setDisabled(true);
   	    	   	  		}
   	    	   	  });
   	    	      var packageId = params['packageId'];
   	    	      checkedIds.packageId = packageId;
   	    	   	  Ext.Ajax.request({		            
				      url:Uec3.App.Constants.homeUrl+"/companyPackage/buyPackage.do",
				      method :"post",					   
				      params:{queryParam:Ext.encode(checkedIds)},
				      timeout:360000,
				      callback :function(options,success,response){
                      },
                      success:function(response,options){
                      	 	var resp = Ext.decode(response.responseText);
				         	Ext.Msg.alert("提示",resp.msg);
				         	if(resp.success){
				         		window.location.href= Uec3.App.Constants.homeUrl+ '/desktop/indexNew.do';		
				         	}
				         	myMask.hide();
                      },
                      failure:function(response,options){
                         	Ext.Msg.alert("提示","发生异常,购买套餐失败,请稍后再试!");
                         	myMask.hide();
                      }
		       	  }); 
   	    	   }  	    	
   	      }); 	   
   	    }
   	    getMarket4RepairStore();
   	    
   		return repairStore_form;
   },
   
   /**
	* Create this modules window here.
	*/
   createWindow : function(params){
    	var desktop = this.app.getDesktop();
        var win = desktop.getWindow('market4RepairStore');
        if (!win) {
			win = desktop.createWindow({
				        id : "market4RepairStore",
						title : '选择搜索市场',
						width : 460,
						height : 270,
						iconCls : 'acc-icon',
					   // minWidth:1000,
						maximizable : true,
						resizable:false,
						maximized:false,
						minimizable:false,
						constrainHeader:true,
						modal:true,
						plain:true,
						// resizable:true,
						//taskbuttonTooltip : "购买套餐",
						layout : "fit",
						items : [this.getComponent(params)]
					});
		}		
    	win.show();         
   }
});